//
// Created by imagination on 4/7/25.
//
#include "setting_display_page.h"

#include "application.h"

#include "lv_i18n/lv_i18n.h"

static int32_t col_desc[] = { LV_GRID_FR(3), LV_GRID_FR(6), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
static int32_t row_desc[] = { LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST };

static lv_obj_t * brig_value_label = NULL;
static lv_obj_t * contrast_value_label = NULL;
static lv_obj_t * hue_value_label = NULL;
static lv_obj_t * saturation_value_label = NULL;

static void brightness_changed_cb(lv_event_t * e) {
    lv_obj_t * obj = lv_event_get_target(e);
    int32_t value = lv_slider_get_value(obj);
    if (app.display_settings.brightness != value) {
        app.display_settings.brightness = value;
        lv_label_set_text_fmt(brig_value_label, "%d", value);
    }
}

static void contrast_changed_cb(lv_event_t * e) {
    lv_obj_t * obj = lv_event_get_target(e);
    int32_t value = lv_slider_get_value(obj);
    if (app.display_settings.contrast != value) {
        app.display_settings.contrast = value;
        lv_label_set_text_fmt(contrast_value_label, "%d", value);
    }
}

static void hue_changed_cb(lv_event_t * e) {
    lv_obj_t * obj = lv_event_get_target(e);
    int32_t value = lv_slider_get_value(obj);
    if (app.display_settings.hue != value) {
        app.display_settings.hue = value;
        lv_label_set_text_fmt(hue_value_label, "%d", value);
    }
}

static void saturation_changed_cb(lv_event_t * e) {
    lv_obj_t * obj = lv_event_get_target(e);
    int32_t value = lv_slider_get_value(obj);
    if (app.display_settings.saturation != value) {
        app.display_settings.saturation = value;
        lv_label_set_text_fmt(saturation_value_label, "%d", value);
    }
}

static void reset_button_clicked_cb(lv_event_t * e) {

}

static void fragment_constructor_ctor(lv_fragment_t * self, void * args) {

}

static lv_obj_t * fragment_create_obj(lv_fragment_t * fragment, lv_obj_t * container) {
    lv_obj_t * root = lv_obj_create(container);
    lv_obj_set_size(root, LV_PCT(100), LV_PCT(100));
    lv_obj_set_style_pad_row(root, LV_DPX(6), LV_PART_MAIN);
    lv_obj_set_layout(root, LV_LAYOUT_GRID);
    lv_obj_set_grid_dsc_array(root, col_desc, row_desc);

    lv_obj_t * label = lv_label_create(root);
    lv_label_set_text(label, _("Brightness:"));
    lv_obj_t * brig_slider = lv_slider_create(root);
    lv_slider_set_range(brig_slider, 0, 100);
    lv_obj_set_height(brig_slider, LV_DPX(32));
    lv_obj_t * value_container = lv_obj_create(root);
    lv_obj_remove_style_all(value_container);
    lv_obj_set_size(value_container, LV_SIZE_CONTENT, LV_DPX(120));
    brig_value_label = lv_label_create(value_container);
    lv_obj_set_align(brig_value_label, LV_ALIGN_CENTER);
    lv_label_set_text_fmt(brig_value_label, "%d", app.display_settings.brightness);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_set_grid_cell(brig_slider, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_set_grid_cell(value_container, LV_GRID_ALIGN_END, 2, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_event_cb(brig_slider, brightness_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    label = lv_label_create(root);
    lv_label_set_text(label, _("Contrast:"));
    lv_obj_t * contrast_slider = lv_slider_create(root);
    lv_slider_set_range(contrast_slider, 0, 100);
    lv_obj_set_height(contrast_slider, LV_DPX(32));
    value_container = lv_obj_create(root);
    lv_obj_remove_style_all(value_container);
    lv_obj_set_size(value_container, LV_SIZE_CONTENT, LV_DPX(120));
    contrast_value_label = lv_label_create(value_container);
    lv_obj_set_align(contrast_value_label, LV_ALIGN_CENTER);
    lv_label_set_text_fmt(contrast_value_label, "%d", app.display_settings.contrast);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(contrast_slider, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(value_container, LV_GRID_ALIGN_END, 2, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_add_event_cb(contrast_slider, contrast_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    label = lv_label_create(root);
    lv_label_set_text(label, _("Hue:"));
    lv_obj_t * hue_slider = lv_slider_create(root);
    lv_slider_set_range(hue_slider, 0, 100);
    lv_obj_set_height(hue_slider, LV_DPX(32));
    value_container = lv_obj_create(root);
    lv_obj_remove_style_all(value_container);
    lv_obj_set_size(value_container, LV_SIZE_CONTENT, LV_DPX(120));
    hue_value_label = lv_label_create(value_container);
    lv_obj_set_align(hue_value_label, LV_ALIGN_CENTER);
    lv_label_set_text_fmt(hue_value_label, "%d", app.display_settings.hue);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    lv_obj_set_grid_cell(hue_slider, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    lv_obj_set_grid_cell(value_container, LV_GRID_ALIGN_END, 2, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    lv_obj_add_event_cb(hue_slider, hue_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    label = lv_label_create(root);
    lv_label_set_text(label, _("Saturation:"));
    lv_obj_t * saturation_slider = lv_slider_create(root);
    lv_obj_set_height(saturation_slider, LV_DPX(32));
    lv_slider_set_range(saturation_slider, 0, 100);
    value_container = lv_obj_create(root);
    lv_obj_remove_style_all(value_container);
    lv_obj_set_size(value_container, LV_SIZE_CONTENT, LV_DPX(120));
    saturation_value_label = lv_label_create(value_container);
    lv_obj_set_align(saturation_value_label, LV_ALIGN_CENTER);
    lv_label_set_text_fmt(saturation_value_label, "%d", app.display_settings.saturation);
    lv_obj_set_style_text_align(saturation_value_label, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 3, 1);
    lv_obj_set_grid_cell(saturation_slider, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 3, 1);
    lv_obj_set_grid_cell(value_container, LV_GRID_ALIGN_END, 2, 1, LV_GRID_ALIGN_CENTER, 3, 1);
    lv_obj_add_event_cb(saturation_slider, saturation_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_obj_t * reset_button = lv_button_create(root);
    lv_obj_t * reset_label = lv_label_create(reset_button);
    lv_obj_set_align(reset_label, LV_ALIGN_CENTER);
    lv_label_set_text(reset_label, _("Reset"));
    lv_obj_set_grid_cell(reset_button, LV_GRID_ALIGN_STRETCH, 0, 3, LV_GRID_ALIGN_CENTER, 4, 1);
    lv_obj_add_event_cb(reset_button, reset_button_clicked_cb, LV_EVENT_CLICKED, 0);

    return root;
}


lv_fragment_class_t setting_display_fragment_cls = {
    .constructor_cb = fragment_constructor_ctor,
    .create_obj_cb = fragment_create_obj,
    .instance_size = sizeof(lv_fragment_t)
};